package com.um.jdy.mp.merchant.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.um.jdy.common.platform.entity.dto.MerchantDTO;
import com.um.jdy.mp.merchant.entity.dto.EditMerchantDTO;
import com.um.jdy.mp.merchant.entity.dto.MerchantPageDTO;
import com.um.jdy.mp.merchant.manager.MerchantManager;
import com.um.jdy.mp.user.entity.vo.MerchantVO;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.controller.BaseController;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 控制器：平台商家表
 */
@Api(tags = "商户管理")
@Validated
@RestController
@RequestMapping("/api/v1/cms/merchant")
@SaCheckLogin
public class MerchantController extends BaseController<MerchantManager> {

    /**
     * 查询分页
     * @param page_no
     * @param page_size
     * @param name
     * @return
     */
    @ApiOperation("分页列表")
    @GetMapping("/page")
    public Response<Page<MerchantVO>> page(@ApiParam(value = "页码") @NotNull Integer page_no,
                                           @ApiParam(value = "数据量") @NotNull Integer page_size,
                                           @ApiParam(value = "用户名称") String name,
                                           @ApiParam(value = "用户类型") String type) {
        PageParams params = new PageParams();
        params.setPageNo(page_no);
        params.setPageSize(page_size);
        if (!StringUtil.isEmpty(name)) {
            params.getCondition().put("m.name", StringUtil.getQueryParam(name));
        }
        if (!StringUtil.isEmpty(type)) {
            params.getCondition().put("m.type", type);
        }
        return manager.page(params);
    }

    /**
     * 查询下级列表
     * @param name
     * @return
     */
    @ApiOperation("下级列表")
    @GetMapping("/sub_list/{type}")
    public Response<List<MerchantVO>> list(@ApiParam(value = "类型") @NotNull @PathVariable String type, @ApiParam(value = "用户名称") String name) {
        return manager.list(type, name);
    }

    @ApiOperation("新增")
    @PostMapping
    public Response<MerchantVO> add(@RequestBody MerchantDTO dto) {
        return manager.addEntity(dto);
    }

    @ApiOperation("更新")
    @PutMapping({"{id}"})
    public Response<MerchantVO> update(@PathVariable String id, @RequestBody EditMerchantDTO dto) {
        return manager.updateEntity(id, dto);
    }

    @ApiOperation("详情")
    @GetMapping({"{id}"})
    public Response<MerchantVO> get(@ApiParam(value = "运营商或商户Id") @NotNull @PathVariable String id) {
        return manager.get(id);
    }
}
